#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <windows.h>
#include "../netlib/netlib.h"

using namespace std;
const int buffer_length = 128;

void __stdcall MyMsgProc(int session_id, void* pMsgBuffer, unsigned long msgID, unsigned long msg_bytes, eOperateType type, int error_code)
{
	if(type == eOperateType::Send)
	{
		if(!error_code)
		{
			cout<<"send msg success!"<<endl;
			std::string msg("abcd");
			send_msg(session_id, 0, msg.length(),(void*)msg.c_str());
		}
		else
		{
			cout<<"parse msg error!"<<endl;
		}
	}

	if(type == eOperateType::Read)
	{
		if(!error_code)
		{
			char* pMsg = new char[msg_bytes + 1];
			memcpy(pMsg, pMsgBuffer, msg_bytes);
			pMsg[msg_bytes] = '\0';
			cout<<pMsg<<endl;
			delete [] pMsg;
		}
	}

	if(type == eOperateType::Connect)
	{
		if(!error_code)
		{
			std::string msg("abcd efg");
			send_msg(session_id, 0, msg.length(), (void*)msg.c_str());
		}
		else
		{
			cout<<"Error Code:"<<error_code<<endl;
		}
	}
}

int main()
{
	create_service();
	initialize_service(2, 100, buffer_length);
	start_service();


	cout<<"start client"<<endl;
	for(int i = 0 ; i < 100; ++i)
	{
		cout<<"start connect:"<<i<<endl;
		start_connect("127.0.0.1", 2233, MyMsgProc);
	}

	getchar();


	close_all_session();
	terminate_service();
	destroy_service();
	return 0;
}
